home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 004 / _spconv / !Spconv / c / Spconv-Fe < prev    next >
Text File  |  1994-09-22  |  13KB  |  465 lines

  1. /* Program: Spconv-FE.c   (Spectrum snapshot converter, front-end)
  2.  *
  3.  * Purpose: Convert Spectrum snapshot files.
  4.  *
  5.  * Details: This is a wimp version for RISC-OS.
  6.  *          This program is a front end to 'ROS-Spconv.c' (a modified version
  7.  *          of 'spconv.c'.
  8.  *          It deals with drag-and-dropping/loading-and-saving of files
  9.  *          before and after calling 'spconv' in 'ROS-Spconv.c'.
  10.  *          The routing 'spconv' in 'ROS-Spconv.c' does the conversion
  11.  *          and this program deals with specifying the input filename
  12.  *          and input/output file types.
  13.  *
  14.  * Author:  RISC-OS conversion: Tim Moore (c) 1994
  15.  *          Original version: Henk de Groot (c)
  16.  *
  17.  */
  18.  
  19. /****** RISC-OS INCLUDE LIBRARIES *******/
  20.  
  21. #include "wimp.h"
  22. #include "wimpt.h"
  23. #include "win.h"
  24. #include "werr.h"
  25. #include "dbox.h"
  26. #include "template.h"
  27. #include "event.h"
  28. #include "baricon.h"
  29. #include "res.h"
  30. #include "resspr.h"
  31. #include "menu.h"
  32. #include "msgs.h"
  33. #include "xferrecv.h"
  34. #include "saveas.h"
  35. #include "os.h"
  36. #include "swis.h"
  37.  
  38. /****** STANDARD ANSI LIBRARIES *********/
  39.  
  40. #include <string.h>
  41. #include <stdlib.h>
  42.  
  43. /****** APPLICATION LIBRARIES ***********/
  44.  
  45. #include "wimputils.h"   /* Some wimp routines */
  46. #include "genutils.h"    /* General utils */
  47. #include "ROS-Spconv.h"  /* The 'Spconv' code */
  48. #include "Spconv-FE.h"   /* Pre-declarations for this source */
  49.  
  50.  
  51. /******************* APPLICATION TYPE DECLARATIONS ************************/
  52.  
  53. /************************** APPLICATION GLOBAL DATA *************************/
  54.  
  55. static int In_FileFormat=NULL;  /* Current settings of the file format */
  56. static int Out_FileFormat=NULL;
  57.  
  58. static char Input_Filename[255];
  59. const char Output_Filename[]="<Wimp$ScrapDir>.NewSnap";
  60.  
  61. /*************************** WIMP GLOBAL DATA *******************************/
  62.  
  63. /* The top of the bar menu tree */
  64. static menu Bar_Menu;
  65.  
  66. /* Handle for the main window */
  67. static wimp_w Main_Window_Handle;
  68.  
  69. /* Flag - is the main window open */
  70. static BOOL Main_Window_Open = FALSE;
  71.  
  72.  
  73. /******************************* MAIN PROGRAM ********************************/
  74.  
  75. /*--- Main entry point. ---*/
  76. int main()
  77. {
  78.   if (Program_Initialise())
  79.   {
  80.     /* The main event loop */
  81.     while (TRUE)
  82.       event_process();
  83.   }
  84.  
  85.   return 0;
  86. }
  87.  
  88. /************************** USER FUNCTIONS/EVENT HANDLERS *********************/
  89.  
  90. /*--- Import file and open main window if neccessary --- */
  91. static void User_Import_File(void)
  92. {
  93.   int filetype;
  94.   char *temp;
  95.  
  96.   filetype=xferrecv_checkinsert(&temp);
  97.  
  98.   strcpy(Input_Filename,temp);
  99.  
  100.   switch (filetype)
  101.   {
  102.     case SNA_FTYPE: In_FileFormat=SNA;
  103.                     User_Open_Window(Main_Window_Handle);
  104.                     break;
  105.     case SP_FTYPE : In_FileFormat=SP;
  106.                     User_Open_Window(Main_Window_Handle);
  107.                     break;
  108.     case Z80_FTYPE: In_FileFormat=Z80;
  109.                     User_Open_Window(Main_Window_Handle);
  110.                     break;
  111.     case PRG_FTYPE: In_FileFormat=PRG;
  112.                     User_Open_Window(Main_Window_Handle);
  113.                     break;
  114.     case ACH_FTYPE: In_FileFormat=ACH;
  115.                     User_Open_Window(Main_Window_Handle);
  116.                     break;
  117.     case KGB_FTYPE: In_FileFormat=KGB;
  118.                     User_Open_Window(Main_Window_Handle);
  119.                     break;
  120.     case UNKNOWN_FTYPE: In_FileFormat=UNKNOWN;
  121.                         User_Open_Window(Main_Window_Handle);
  122.                         break;
  123.  
  124.     default: In_FileFormat=NULL;
  125.              break;
  126.   }
  127.   xferrecv_insertfileok();
  128. }
  129.  
  130. static BOOL User_Save_File (char *filename, void *handle)
  131. {
  132.   int FileType=User_File_Format_To_FType(Out_FileFormat);
  133.  
  134.   handle=handle;
  135.  
  136.   /* Copy file from Wimp$ScrapDir (deleting the original) to destination
  137.      in 'filename' */
  138.  
  139.   if (!Genutils_Copy_File((char *) Output_Filename,filename)) {
  140.      werr(FALSE,"Problems copying file to destination");
  141.      return FALSE;
  142.   }
  143.  
  144.   /* Now set filetype */
  145.   if (!Genutils_Set_File_Type(filename,FileType)) {
  146.      werr(FALSE,"Problem setting the destination file type");
  147.      return FALSE;
  148.   }
  149.   else
  150.      return TRUE;
  151. }
  152.  
  153. static int User_File_Format_To_FType(int FileFormat)
  154. {
  155.   switch (FileFormat) {
  156.      case SP: return SP_FTYPE; break;
  157.      case SNA: return SNA_FTYPE; break;
  158.      case Z80: return Z80_FTYPE; break;
  159.      case PRG: return PRG_FTYPE; break;
  160.      case ACH: return ACH_FTYPE; break;
  161.      case KGB: return KGB_FTYPE; break;
  162.      default: break;
  163.   }
  164.   return UNKNOWN_FTYPE;
  165. }
  166.  
  167. static void User_Save_As()
  168. {
  169.  
  170.   int File_Type=User_File_Format_To_FType(Out_FileFormat);
  171.   
  172.   if (!saveas(File_Type, "Snapshot", 67584, User_Save_File, 0, 0, 0))
  173.      werr(FALSE,"Error with SaveAs dialogue");
  174. }
  175.  
  176. /*--- Determine selected option in main window ---*/
  177. static int User_Get_Save_Option(void)
  178. {
  179.  
  180.   if (Wimp_Utils_Isselected(Main_Window_Handle, SP_OPTION)) return SP;
  181.   if (Wimp_Utils_Isselected(Main_Window_Handle, SNA_OPTION)) return SNA;
  182.   if (Wimp_Utils_Isselected(Main_Window_Handle, Z80_OPTION)) return Z80;
  183.   if (Wimp_Utils_Isselected(Main_Window_Handle, PRG_OPTION)) return PRG;
  184.   if (Wimp_Utils_Isselected(Main_Window_Handle, ACH_OPTION)) return ACH;
  185.   if (Wimp_Utils_Isselected(Main_Window_Handle, KGB_OPTION)) return KGB;
  186.  
  187.   /* Default, this should never be reached */
  188.   return ACH;
  189. }
  190.  
  191. /*--- Actions to perform on opening a window ---*/
  192. static void User_Open_Window(wimp_w Handle)
  193. {
  194.   if (Handle == Main_Window_Handle)
  195.   {
  196.      Main_Window_Open=Wimp_Utils_Open_Window( Main_Window_Handle,TRUE,
  197.                                                     Main_Window_Open );
  198.   }
  199. }
  200.  
  201. /*--- Actions to perform on closing a window ---*/
  202. static void User_Close_Window(wimp_w Handle)
  203. {
  204.   if (Handle == Main_Window_Handle)
  205.   {
  206.      wimpt_noerr(wimp_close_wind(Handle));
  207.      Main_Window_Open=FALSE;
  208.   }
  209. }
  210.  
  211. /*--- User clicked a mouse button somewhere, now we deal with it ---*/
  212. static void User_Button_Press(wimp_i Icon, wimp_w Handle)
  213. {
  214.   int result;
  215.  
  216.   if (Handle == Main_Window_Handle)
  217.   {
  218.     switch (Icon)
  219.     {
  220.  
  221.       case PROCEED_ICON: Out_FileFormat=User_Get_Save_Option();
  222.                          result=spconv(Input_Filename, In_FileFormat, Out_FileFormat);
  223.                          if (result == EXIT_OK) User_Save_As();
  224.                          remove(Output_Filename);
  225.                          User_Close_Window(Main_Window_Handle);
  226.                          In_FileFormat=NULL;
  227.                          break;
  228.  
  229.       case CANCEL_ICON: User_Close_Window(Main_Window_Handle);
  230.                         In_FileFormat=NULL;
  231.                         break;
  232.       default:
  233.          break;
  234.     }
  235.   }
  236. }
  237.  
  238. /*--- Event handler called on a left click on the icon-bar icon. ---*/
  239. static void User_Bar_Iconclick(wimp_i Icon)
  240. {
  241.   Icon = Icon; /* We don't need the handle: this stops compiler warning */
  242. }
  243.  
  244. /*--- A request for help is dealt with here ---*/
  245. static void User_Process_Help(wimp_msgstr *Msg)
  246. {
  247.     char Help_Lookup[10];
  248.  
  249.     /* Locate the window pointer is currently over */
  250.     if (Msg->data.helprequest.m.w == Main_Window_Handle)
  251.     {
  252.         /* Now the icon(s) or just the window on its own */
  253.         switch (Msg->data.helprequest.m.i) {
  254.         case 1: case 2: case 3: case 4:
  255.         case 5: case 6: case 8: case 10:
  256.               sprintf(Help_Lookup,"HMWINDI%d",Msg->data.helprequest.m.i);
  257.               break;
  258.  
  259.         default:
  260.               sprintf(Help_Lookup,"HMWIND");
  261.               break;
  262.         }
  263.  
  264.         Wimp_Utils_Help(msgs_lookup(Help_Lookup),Msg);
  265.     }
  266.     else
  267.         /* Pointer must be over the icon bar */
  268.         Wimp_Utils_Help(msgs_lookup("IBARHELP"),Msg);
  269. }
  270.  
  271. /*--- A keyboard event is dealt with here ---*/
  272. static void User_Keyboard_Hit(wimp_caretstr c, int Chcode)
  273. {
  274.     c=c; /* Un-needed handle */
  275.  
  276.     switch (Chcode)
  277.     {
  278.  
  279.         /**** Need one for RETURN as with clicking on OK ****/
  280.  
  281.         default:
  282.             wimp_processkey(Chcode);  /* Pass unknown key events back to wimp */
  283.             break;
  284.     }
  285. }
  286.  
  287. /*--- Create a dialogue box for program information details ---*/
  288. static void User_Prog_Info(void)
  289. {
  290.     Version_Info The_Version_Info;
  291.  
  292.     strcpy(The_Version_Info.Version,msgs_lookup("Version"));
  293.     The_Version_Info.Version_Ic=INFO_VERSION_ICON;
  294.     Wimp_Utils_Program_Info("ProgInfo",The_Version_Info);
  295. }
  296.  
  297. /****************************** MENU HANDLING **************************/
  298.  
  299. /*--- Event handler for the bar menu. ---*/
  300. static void User_Bar_Menuproc(void *Handle, char *Hit)
  301. {
  302.   char Help_Command[80];
  303.   
  304.   Handle = Handle; /* We don't need handle: this stops compiler warning */
  305.   
  306.   /* Find which menu item was hit and take action as appropriate */
  307.   switch (Hit[0])
  308.   {
  309.     case BAR_MENU_INFO:
  310.       User_Prog_Info();
  311.       break;
  312.  
  313.     case BAR_MENU_HELP:
  314.       sprintf(Help_Command,"%s",msgs_lookup("HCommand"));
  315.       os_cli(Help_Command);
  316.       break;
  317.       
  318.     case BAR_MENU_QUIT:
  319.       /* Exit from the program. The wimp gets rid of the window and icon */
  320.       exit(0);
  321.   }
  322. }
  323.  
  324. /*--- Event handler for the main menu. ---*/
  325. static void User_Main_Menuproc(void *Handle, char *Hit)
  326. {
  327.   char Help_Command[80];
  328.   Handle=Handle;
  329.  
  330.  
  331.   /* Find which menu item was hit and take action as appropriate */
  332.   switch (Hit[0])
  333.   {
  334.     case MAIN_MENU_INFO:
  335.       User_Prog_Info();
  336.       break;
  337.  
  338.     case MAIN_MENU_HELP:
  339.       sprintf(Help_Command,"%s",msgs_lookup("HCommand"));
  340.       os_cli(Help_Command);
  341.       break;
  342.  
  343.   }
  344. }
  345.  
  346.  
  347. /*--- Menu Maker (Create a menu for the main window only when needed ---*/
  348. static menu User_Main_Window_Menumaker(void *Handle)
  349. {
  350.   menu Main_Menu;
  351.  
  352.   Handle = Handle;
  353.   if (!event_is_menu_being_recreated())
  354.   {
  355.     /* Create the main window menu tree */
  356.        Main_Menu = menu_new(msgs_lookup("Men1Title"), msgs_lookup("Menu1"));
  357.  
  358.   }
  359.  
  360.   return Main_Menu;
  361.  
  362. }
  363.  
  364. /****************************** EVENT HANDLERS ******************************/
  365.  
  366. /*---  Window event handler, used for all windows ---*/
  367. static void Window_Event_Handler(wimp_eventstr *e, void *Handle)
  368. {
  369.  
  370.   Handle = Handle; /* We don't need handle: this stops compiler warning */
  371.  
  372.   /* Deal with event */
  373.   switch (e->e)
  374.   {
  375.     case wimp_EREDRAW:
  376.       Wimp_Utils_Redraw_Window(e->data.o.w);
  377.       break;
  378.  
  379.     case wimp_EOPEN:
  380.       wimpt_noerr(wimp_open_wind(&e->data.o));
  381.       break;
  382.     
  383.     /* Do this under the 'cancel' button, there's no 'close' icon */
  384.     case wimp_ECLOSE:  /* Pass on close request */
  385.       User_Close_Window(e->data.o.w); /* window to close */
  386.       break;
  387.  
  388.     case wimp_EBUT:  /* Check for button presses */
  389.       if ((e->data.but.m.bbits & wimp_BLEFT)||(e->data.but.m.bbits &
  390.                     wimp_BRIGHT))
  391.          User_Button_Press(e->data.but.m.i, e->data.but.m.w); /* Icon and
  392.                                                                  window */
  393.       break;
  394.  
  395.     case wimp_ESEND:
  396.     case wimp_ESENDWANTACK: /* Process broadcasts */
  397.         switch (e->data.msg.hdr.action)
  398.         {
  399.             case wimp_MDATALOAD:
  400.                 User_Import_File(); /* Check imported file type and open
  401.                                        conversion window if neccessary */
  402.                 break;
  403.  
  404.             case wimp_MHELPREQUEST:
  405.                 User_Process_Help(&e->data.msg);
  406.                 break;
  407.                 
  408.             default:
  409.                 break;
  410.         }
  411.         break;
  412.  
  413.     case wimp_EKEY: /* Process keyboard events */
  414.         User_Keyboard_Hit(e->data.key.c, e->data.key.chcode);
  415.         break;
  416.  
  417.     default:   /* Ignore any other event */
  418.       break;
  419.   }
  420. }
  421.  
  422. /****************************** INITIALISATION ******************************/
  423.  
  424. /*--- Initialise the program, returning TRUE if it was all OK. ---*/
  425. static BOOL Program_Initialise(void)
  426. {
  427.   /* RISC_OSlib initialisation */
  428.   wimpt_init("Spconv");         /* Main Wimp initialisation */
  429.   res_init("Spconv");           /* Resources */
  430.  
  431.   resspr_init();                   /* Application Sprites */
  432.  
  433.   template_init();                 /* Templates */
  434.   dbox_init();                     /* Dialogue boxes */
  435.   msgs_init();                     /* Initialise message routines */
  436.  
  437.   /* Create the main window, and declare its event handler */
  438.   if (!Wimp_Utils_Create_Window("Main", &Main_Window_Handle))
  439.     return FALSE; /* Window creation failed */
  440.   win_register_event_handler(Main_Window_Handle, Window_Event_Handler, 0);
  441.  
  442.  
  443.   /* Create the menu bar tree */
  444.   if (Bar_Menu = menu_new(msgs_lookup("MMenTitle"), msgs_lookup("MMenu")),
  445.     Bar_Menu == NULL)
  446.     return FALSE; /* Menu create failed */
  447.  
  448.   /* attach a menumaker to the main window */
  449.   if (!event_attachmenumaker(Main_Window_Handle,User_Main_Window_Menumaker,
  450.     User_Main_Menuproc, 0))
  451.     return FALSE; /* Unable to attach menumaker */
  452.  
  453.   /* Set up the icon on the icon bar, and declare its event handlers */
  454.   baricon("!spconv", (int)resspr_area(), User_Bar_Iconclick);
  455.   if (!event_attachmenu(win_ICONBAR, Bar_Menu, User_Bar_Menuproc, 0))
  456.     return FALSE; /* Unable to attach menu */
  457.  
  458.   /* Attach other events to window event handler */
  459.   win_claim_unknown_events(Main_Window_Handle);
  460.  
  461.  
  462.   /* All went ok */
  463.   return TRUE;
  464. }
  465.